package com.atuigu.test;

import com.atguigu.bean.Car;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 伟哥
 * @date 2021-04-29 10:12
 */
public class IOCTest5 {

    private ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc5.xml");

    /**
     * 实验15  Spring管理连接池
     */
    @Test
    public void test15(){

        /*
        这样获取，需要强转；通过类型获取不需要强转
        DataSource dataSource = (DataSource) ioc.getBean("dataSource");
         */

        // 通过类型获取ioc容器内的bean
        DataSource dataSource = ioc.getBean(DataSource.class);

        // 获取一条连接，打印此链接，查看是否正常
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println(connection);


        /*
        * 测试xml配置文件中的 ${username}默认值，是本机电脑的名字HelloKitty
        * */
        Car bean = ioc.getBean(Car.class);
        System.out.println(bean);

    }
}
